package com.cnswhy.cloud.server.merchant.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cnswhy.cloud.common.core.base.R;
import com.cnswhy.cloud.common.core.base.controller.SuperController;
import com.cnswhy.cloud.server.core.dto.CustomerUpgradeTransOrderPageDTO;
import com.cnswhy.cloud.server.core.dto.CustomerUpgradeTransOrderSaveDTO;
import com.cnswhy.cloud.server.core.dto.CustomerUpgradeTransOrderUpdateDTO;
import com.cnswhy.cloud.server.core.entity.CustomerUpgradeTransOrder;
import com.cnswhy.cloud.server.core.service.CustomerUpgradeMenuService;
import com.cnswhy.cloud.server.core.service.CustomerUpgradeTransOrderService;
import com.cnswhy.cloud.server.core.service.customer.CustomerService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 
 * </p>
 *
 * @author cnswhy
 * @date 2021-07-05
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/customerUpgradeTransOrder")
@Api(value = "CustomerUpgradeTransOrder", tags = "")
//@PreAuth(replace = "customerUpgradeTransOrder:")
public class CustomerUpgradeTransOrderController extends SuperController<CustomerUpgradeTransOrderService, Long, CustomerUpgradeTransOrder, CustomerUpgradeTransOrderPageDTO, CustomerUpgradeTransOrderSaveDTO, CustomerUpgradeTransOrderUpdateDTO> {
    @Autowired
    CustomerService customerService;
    @Autowired
    CustomerUpgradeMenuService customerUpgradeMenuService;
    @Override
    public void handlerResult(IPage<CustomerUpgradeTransOrder> page) {
        for(CustomerUpgradeTransOrder customerUpgradeTransOrder : page.getRecords()){
            customerUpgradeTransOrder.setCustomerUpgradeMenu(customerUpgradeMenuService.getById(customerUpgradeTransOrder.getMenuId()));
            customerUpgradeTransOrder.setCustomer(customerService.getById(customerUpgradeTransOrder.getCustomerId()));
        }
    }
    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<CustomerUpgradeTransOrder> customerUpgradeTransOrderList = list.stream().map((map) -> {
            CustomerUpgradeTransOrder customerUpgradeTransOrder = CustomerUpgradeTransOrder.builder().build();
            return customerUpgradeTransOrder;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(customerUpgradeTransOrderList));
    }
}
